package com.atguigu.gmall.mq.config;


import org.springframework.amqp.core.*;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DelayMqConfig {

    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";


    @Bean  //创建交换机
    public Exchange exchange(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(
                exchange_delay,//交换机名称
                "x-delayed-message",  //类型
                true, //是否持久化
                false, //是否自动删除
                args    //路由模式
                );
    }

    @Bean  //创建队列
    public Queue queue1(){
        return  QueueBuilder.durable(queue_delay_1).build();
    }

    @Bean  //绑定 交换机绑定队列
    public Binding binding(@Qualifier(value = "queue1") Queue queue,@Qualifier(value = "exchange") Exchange exchange){
        //参数1 创建的队列方法  参数2  创建交换机的方法 参数3 routingKey
        return BindingBuilder.bind(queue).to(exchange).with(routing_delay).noargs();
    }

}
